<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/Entite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/database/Database.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/exceptions/DAOException.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/uc/UcEntite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/string/StringUtils.php';

class UcDAO {
	
	private function UcDAO() {
		//évite la construction
	}
		
	public static function getListeUc() {
		$database = Database::getInstance();
		$result = $database->executeQueryAndClose("SELECT id, code, libelle FROM uc ORDER BY libelle");
		$ucArray = Array();
		foreach ($result as $row) {
			$ucArray[] = UcDAO::getUcsFromDatabaseRow($row);
		}
		return $ucArray;
	}
	
	
	
	/*public static function getListeUcById($listeIdUc) {
		if (count($listeIdUc) == 0) {
			return Array();
		}
		$database = Database::getInstance();
		$listeIdAsString = StringUtils::getArrayAsString($listeIdUc);
		$result = $database->executeQueryAndClose("SELECT id, code, libelle FROM uc WHERE id IN ($listeIdAsString) ORDER BY libelle");
		$ucArray = Array();
		foreach ($result as $row) {
			$ucArray[] = UcDAO::getUcsFromDatabaseRow($row);
		}
		return $ucArray;
	}*/
	
	public static function getUcByID($id) {
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, code, libelle FROM uc WHERE id =$id");
		foreach ($result as $row){
			return UcDAO::getUcsFromDatabaseRow($row);
		}
		throw new MissingDataException("Impossible de trouver l'activité avec l'ID ${id}");
	}
	
	public static function save(UcEntite $ucEntite) {
		if($ucEntite->id == Entite::getUnpersistedId()) {
			$result = UcDAO::insert($ucEntite);
		} else {
			$result = UcDAO::update($ucEntite);
			if ($result > 1) {
				$libelle = $ucEntite->libelle;
				throw new DAOException("La sauvegarde de l'activité '$libelle' a mis à jour $result lignes");
			}
		}
	}
	
	public static function delete($id) {
		$result = Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM uc WHERE id = $id") ;
		return $result;
	}
	
	private static function insert(UcEntite $ucEntite) {
		$libelle = mysql_escape_string($ucEntite->libelle);
		$code = mysql_escape_string($ucEntite->code);
		
		$result = Database::getInstance()->executeInsertQuery("INSERT INTO uc (code, libelle) values ('$code', '$libelle')") ;
		return $result;
	}
	
	private static function update(UcEntite $ucEntite) {
		$libelle = mysql_escape_string($ucEntite->libelle);
		$code = mysql_escape_string($ucEntite->code);
		$result = Database::getInstance()->executeUpdateQueryAndClose("UPDATE uc SET code='$code', libelle = '$libelle' WHERE id = $ucEntite->id");
		return $result;
	}
	
	private static function getUcsFromDatabaseRow($row) {
		$ucEntite = new UcEntite();
		$ucEntite->id = $row['id'];
		$ucEntite->code = $row['code'];
		$ucEntite->libelle = $row['libelle'];
		return $ucEntite;
	}
	
	public static function getListeIdRoleAssocieAUc($idUc) {
		$database = Database::getInstance();
		$result = $database->executeQueryAndClose("SELECT id_role FROM uc_role WHERE id_uc = $idUc");
		$idRoleArray = Array();
		foreach ($result as $row) {
			$idRoleArray[] = $row['id_role'];
		}
		return $idRoleArray;
	}
	
	public static function saveListeIdRole($uc, $listeIdRole) {
		//Supprime l'ancienne liste de rôles
		Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM uc_role WHERE id_uc = $uc->id") ;
	
		//ajoute les nouveaux rôles
		foreach($listeIdRole as $idRole) {
			Database::getInstance()->executeInsertQuery("INSERT INTO uc_role (id_uc,id_role) values ($uc->id,$idRole)") ;
		}
	}
}
?>